// brandManage/pages/orderManage/orderManage.js
var common = require("../../../common/common.js").common;

var leader = require("../../../common/leader.js").leader;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        goodsListStyle: {
            style: 4,
            styleConfig: {
                backgroundColor: "#fff"
            }
        },
        choose_nav: 0,
        type: 1,
        page: 1,
        keyword: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            esb_id: options.id,
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getBase();
        this.getData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getData();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    chooseType: function(e) {
        this.setData({
            type: e.currentTarget.dataset.id,
            choose_nav: e.currentTarget.dataset.id == 1 ? 0 : -1,
            page: 1,
            order: []
        });
        this.getData();
    },
    chooseStatus: function(e) {
        this.setData({
            choose_nav: e.currentTarget.dataset.id,
            order: [],
            page: 1
        });
        this.getData();
    },
    saveWord: function(e) {
        this.setData({
            keyword: e.detail.value,
            page: 1,
            order: []
        });
        this.getData();
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var goodsListStyle = that.data.goodsListStyle;
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    leader_change: res.extra.l_change,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    timeStyle: {
                        height: 32,
                        mainColor: res.extra.color_main
                    },
                    goodsListStyle: {
                        ...goodsListStyle,
                        mainColor: res.extra.color_main
                    },
                    leader_address_on: res.extra.leader_address_on
                });
            }
        });
    },
    getData: function() {
        if (this.data.type == 2) {
            this.getRefundData();
        } else {
            this.getOrdinaryData();
        }
    },
    getOrdinaryData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var choose_nav = that.data.choose_nav;
        var esb_id = that.data.esb_id;
        var page = that.data.page;
        common.request({
            endpoint: "My/myinfo/myorder_all2",
            method: "post",
            data: {
                openid: openid,
                status: choose_nav,
                page: page,
                esb_id: esb_id
            },
            success: res => {
                var data = that.data.order;
                //解构
                                var newData = that.orderList(res.extra);
                let order = page == 1 ? newData : data.concat(newData);
                if (res.extra.length > 0) {
                    console.log(res);
                    that.setData({
                        order: order
                    });
                    that.data.page = page + 1;
                }
            }
        });
    },
    getRefundData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var choose_nav = that.data.choose_nav;
        var esb_id = that.data.esb_id;
        var keyword = that.data.keyword;
        var page = that.data.page;
        common.request({
            endpoint: "My/myinfo/my-after-order",
            method: "post",
            data: {
                openid: openid,
                after_status: choose_nav,
                page: page,
                esb_id: esb_id,
                keyword: keyword
            },
            success: res => {
                var data = that.data.order;
                var newData = res.extra;
                let order = page == 1 ? newData : data.concat(newData);
                if (res.extra.length > 0) {
                    console.log(res);
                    that.setData({
                        order: order
                    });
                    that.setData({
                        page: page + 1
                    });
                }
            }
        });
    },
    orderList(data) {
        if (data || data.length > 0) {
            data.forEach((item, index) => {
                var newProArry = [];
                var deliveryCost = [];
                var aftermarket_num = 0;
                //可售后子单数量
                                var delivery1_num = 0;
                //自提订单数量
                                var delivery2_num = 0;
                //团长配送订单数量
                                var delivery3_num = 0;
                //平台配送订单数量
                                var status4_num = 0;
                //待取货订单数量 
                // 拼运费字符串
                                if (item.esb_delivery && item.esb_delivery != "0.00") {
                    deliveryCost.push("平台配送费：￥" + item.esb_delivery);
                }
                if (item.express_delivery && item.express_delivery != "0.00") {
                    deliveryCost.push("快递配送费：￥" + item.express_delivery);
                }
                if (item.leader_delivery && item.leader_delivery != "0.00") {
                    deliveryCost.push("团长配送费：￥" + item.leader_delivery);
                }
                if (item.packing_fee) {
                    deliveryCost.push("包装费：￥" + parseFloat(item.packing_fee).toFixed(2));
                }
                item.item.forEach((proInfo, proIndx) => {
                    //统计可售后子单数量
                    if (proInfo.aftermarket_overtime == 1) {
                        aftermarket_num++;
                    }
                    if (proInfo.order_status == "4") {
                        status4_num++;
                    }
                    switch (proInfo.delivery_channel) {
                      case "1":
                        delivery1_num++;
                        break;

                      case "2":
                        delivery2_num++;
                        break;

                      case "3":
                        delivery3_num++;
                        break;
                    }
                    var pro = {
                        id: proInfo.id,
                        //订单id，
                        proId: proInfo.pro_id,
                        //商品id
                        icon: proInfo.icon,
                        //商品缩略图
                        proName: proInfo.alias_name,
                        //商品名称
                        spec: proInfo.product_info,
                        //规格
                        money_refund: proInfo.money_refund,
                        //是否是满减商品
                        price: proInfo.price,
                        //商品单价
                        num: proInfo.num,
                        //数量
                        status: proInfo.status,
                        //售后状态，-1——仅退款，-2—— 退货退款，-3退货换货
                        order_status: proInfo.order_status,
                        //订单状态
                        isExpressPro: proInfo.delivery_channel == 4 ? 1 : 0,
                        //是否是快递商品，1是，2否
                        order_status: proInfo.order_status,
                        //订单状态
                        payment_channel: item.payment_channel
                    };
                    newProArry.push(pro);
                });
                var newOrder = {
                    totalNum: item.allNum,
                    item: newProArry
                };
                item.goodsListData = newOrder;
                item.deliveryCost = "(" + (deliveryCost.length > 0 ? deliveryCost.join(",") : "免配送费") + ")";
                item.aftermarket_num = aftermarket_num;
                item.delivery1_num = delivery1_num;
                item.delivery2_num = delivery2_num;
                item.delivery3_num = delivery3_num;
                item.status4_num = status4_num;
            });
            return data;
        }
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});